using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LiveSearchAPITest.com.msn.search.soap;
using System.Collections.ObjectModel;

namespace LiveSearchAPITest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {
        private MSNSearchService searchService = new MSNSearchService();
        private SearchRequest searchRequest = new SearchRequest();
        private SourceRequest[] sourceRequest = new SourceRequest[1];
        private ListView listView;

        public Window1()
        {
            InitializeComponent();
            button1.Click += new RoutedEventHandler(OnSearchClick);
            this.Loaded += new RoutedEventHandler(OnLoad);

            searchRequest.AppID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            searchRequest.CultureInfo = "en-US";
            SourceRequestInitialization();
       }

        void OnLoad(object sender, RoutedEventArgs args)
        {
            listView = new ListView();
            GridView gridView = new GridView();

            gridView.AllowsColumnReorder = true;
            gridView.ColumnHeaderToolTip = "Results";

            GridViewColumn gridViewColumn1 = new GridViewColumn();
            gridViewColumn1.DisplayMemberBinding = new Binding("Title");
            gridViewColumn1.Header = "Title";
            gridViewColumn1.Width = 200;
            GridViewColumn gridViewColumn2 = new GridViewColumn();
            gridViewColumn2.DisplayMemberBinding = new Binding("Description");
            gridViewColumn2.Header = "Description";
            gridViewColumn2.Width = 300;
            GridViewColumn gridViewColumn3 = new GridViewColumn();
            gridViewColumn3.DisplayMemberBinding = new Binding("Url");
            gridViewColumn3.Header = "Url";
            gridViewColumn3.Width = 200;

            gridView.Columns.Add(gridViewColumn1);
            gridView.Columns.Add(gridViewColumn2);
            gridView.Columns.Add(gridViewColumn3);
            listView.View = gridView;
            myStackPanel.Children.Add(listView);
        }

        private void SourceRequestInitialization()
        {
            sourceRequest[0] = new SourceRequest();
            sourceRequest[0].Source = SourceType.Web;
            sourceRequest[0].ResultFields = ResultFieldMask.Description | ResultFieldMask.DisplayUrl | ResultFieldMask.Title | ResultFieldMask.Url;
            searchRequest.Requests = sourceRequest;
        }


        void OnSearchClick(object sender, RoutedEventArgs args)
        {
            List<Item> list = new List<Item>();
            searchRequest.Query = textBox1.Text;
            SearchResponse searchResponse = searchService.Search(searchRequest);
            foreach (Result sourceResult in searchResponse.Responses[0].Results)
            {
                string title, description, url;
                title = (!String.IsNullOrEmpty(sourceResult.Title)) ? sourceResult.Title : String.Empty;
                description = (!String.IsNullOrEmpty(sourceResult.Description)) ? sourceResult.Description : String.Empty;
                url = (!String.IsNullOrEmpty(sourceResult.Description)) ? sourceResult.Url : String.Empty;
                list.Add(new Item(title, description, url));
                Console.WriteLine("Title: {0}\nDescription: {1}\nUrl: {2}\n", title, description, url);
            }
            listView.ItemsSource = new Items(list);
        }
    }

    public class Item
    {
        private string title;
        private string description;
        private string url;

        public Item(string title, string description, string url)
        {
            this.title = title;
            this.description = description;
            this.url = url;
        }

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public string Url
        {
            get { return url; }
            set { url = value; }
        }
    }

    public class Items : ObservableCollection<Item>
    {
        public Items(List<Item> list)
        {
            foreach (Item item in list)
            {
                this.Add(item);
            }
        }
    }
}
